package com.yc.bdw.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.yc.bdw.R;
import com.yc.bdw.model.Address;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AddressListAdapter extends BaseAdapter {

	private static HashMap<Integer,Boolean> isSelected;

	private List<Address> list = new ArrayList<Address>();
	private LayoutInflater inflater = null;
	private Context context = null;

	private int which = -1;

	public AddressListAdapter(){}

	public AddressListAdapter(Context context) {
		inflater = LayoutInflater.from(context); 
		this.context = context;
	}

	public void init() {
		isSelected = new HashMap<Integer, Boolean>();
		for ( int i = 0; i < list.size(); i++ ) {
			isSelected.put(i,false);
		}
	}
	
	@Override
	public int getCount() {
		if ( list != null ) {
			return list.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public static class AddressItem {
		TextView name;
		TextView phone;
		TextView address;
		CheckBox select;
		ImageView modify;
		ImageView delete;
		ImageView isDefault;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		AddressItem addressItem;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_address, null);
			addressItem = new AddressItem();
			addressItem.name = (TextView) convertView.findViewById(R.id.item_address_name);
			addressItem.phone = (TextView) convertView.findViewById(R.id.item_address_phone);
			addressItem.address = (TextView) convertView.findViewById(R.id.item_address_address);
			addressItem.select = (CheckBox) convertView.findViewById(R.id.item_address_select);
			addressItem.modify = (ImageView) convertView.findViewById(R.id.item_address_modify);
			addressItem.delete = (ImageView) convertView.findViewById(R.id.item_address_delete);
			addressItem.isDefault = (ImageView) convertView.findViewById(R.id.item_address_default);

			convertView.setTag(addressItem);
		} else {
			addressItem = (AddressItem) convertView.getTag();
		}

		addressItem.name.setText(list.get(position).getName());
		addressItem.phone.setText(list.get(position).getPhone());
		addressItem.address.setText(list.get(position).getAddress()+list.get(position).getDetailAddress());

		if ( list.get(position).isDefault() ) {
			addressItem.isDefault.setVisibility(View.VISIBLE);
		} else {
			addressItem.isDefault.setVisibility(View.GONE);
		}

		addressItem.select.setChecked(isSelected.get(position));
		addressItem.select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton compoundButton, boolean flag) {
				if (flag) {
					which = position;
					for (int i = 0; i < isSelected.size(); i++) {
						if (position == i) {
							isSelected.put(i, true);
						} else {
							isSelected.put(i, false);
						}
					}
					notifyDataSetChanged();
				}
			}
		});

		addressItem.modify.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {

			}
		});

		addressItem.delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				list.remove(position);
				isSelected.remove(position);
				for ( int i = isSelected.size(); i > position; i-- ) {
					isSelected.put(i-1,isSelected.get(i));
				}
				notifyDataSetChanged();
			}
		});

		return convertView;
	}
	
	public List<Address> getList() {
		return list;
	}

	public void setList(List<Address> list) {
		this.list = list;
	}

	public int getWhich() {
		return which;
	}

	public void setWhich(int which) {
		this.which = which;
	}
}